﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class team_index : FBasePage
{
    // Fields
    private DateTime beginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");

    private DateTime endTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

    private bool result = false;
    private string state = "";
    public string strNav = "";
    public string strTitle = "";

    // Methods
    private void initNav()
    {
        Maticsoft.BLL.Category category;
        DataSet list;
        int num;
        DataRow row;
        StringBuilder sb = new StringBuilder();
        string str = "";
        if ((base.Request["gid"] == null) || (base.Request["gid"].ToString() == ""))
        {
            str = "";
            if ((base.Request["typeid"] == null) || (base.Request["typeid"].ToString() == ""))
            {
                sb.Append("<li class=\"current\"><a href=\"/team/index.aspx\">所有</a><span></span></li>");
            }
            else
            {
                sb.Append("<li ><a href=\"/team/index.aspx\">所有</a><span></span></li>");
            }
            this.strTitle = "所有往期团购";
            this.initNav(sb);
            category = new Maticsoft.BLL.Category();
            if ((base.ASSystem != null) && (base.ASSystem.cateteam == 1))
            {
                list = category.GetList(" Zone='group' and Display='Y' order by Sort_order desc");
                if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
                {
                    for (num = 0; num < list.Tables[0].Rows.Count; num++)
                    {
                        row = list.Tables[0].Rows[num];
                        sb.Append("<li ");
                        if (str == row["id"].ToString())
                        {
                            sb.Append("class=\"current\" ");
                        }
                        sb.Append(" ><a href=\"/team/index.aspx?gid=" + row["id"].ToString() + "\">" + row["Name"].ToString() + "</a><span></span></li>");
                    }
                }
            }
        }
        else
        {
            str = base.Request["gid"].ToString();
            sb.Append("<li ><a href=\"/team/index.aspx\">所有</a><span></span></li>");
            this.initNav(sb);
            category = new Maticsoft.BLL.Category();
            if (base.ASSystem != null)
            {
                if (base.ASSystem.Displayfailure == 1)
                {
                    this.result = true;
                }
                if (base.ASSystem.cateteam == 1)
                {
                    list = category.GetList(" Zone='group' and Display='Y' order by Sort_order desc");
                    if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
                    {
                        for (num = 0; num < list.Tables[0].Rows.Count; num++)
                        {
                            row = list.Tables[0].Rows[num];
                            sb.Append("<li ");
                            if (str == row["id"].ToString())
                            {
                                sb.Append("class=\"current\" ");
                                this.strTitle = row["Name"].ToString() + "往期团购";
                            }
                            sb.Append(" ><a href=\"/team/index.aspx?gid=" + row["id"].ToString() + "\">" + row["Name"].ToString() + "</a><span></span></li>");
                        }
                    }
                }
            }
        }
        this.strNav = sb.ToString();
    }

    private void initNav(StringBuilder sb)
    {
        if ((base.Request["typeid"] == null) || (base.Request["typeid"].ToString() == ""))
        {
            sb.Append("<li><a href=\"/team/index.aspx?typeid=1\">今日团购</a><span></span></li>");
            sb.Append("<li><a href=\"/team/index.aspx?typeid=3\">成功团购</a><span></span></li>");
            if ((base.ASSystem != null) && (base.ASSystem.Displayfailure == 1))
            {
                sb.Append("<li><a href=\"/team/index.aspx?typeid=2\">失败团购</a><span></span></li>");
            }
        }
        else
        {
            if (base.Request["typeid"].ToString() == "1")
            {
                sb.Append("<li class=\"current\"><a href=\"/team/index.aspx?typeid=1\">今日团购</a><span></span></li>");
                sb.Append("<li><a href=\"/team/index.aspx?typeid=3\">成功团购</a><span></span></li>");
                if ((base.ASSystem != null) && (base.ASSystem.Displayfailure == 1))
                {
                    sb.Append("<li><a href=\"/team/index.aspx?typeid=2\">失败团购</a><span></span></li>");
                }
                this.strTitle = "今日往期团购";
            }
            if (base.Request["typeid"].ToString() == "3")
            {
                sb.Append("<li ><a href=\"/team/index.aspx?typeid=1\">今日团购</a><span></span></li>");
                sb.Append("<li class=\"current\"><a href=\"/team/index.aspx?typeid=3\">成功团购</a><span></span></li>");
                if ((base.ASSystem != null) && (base.ASSystem.Displayfailure == 1))
                {
                    sb.Append("<li><a href=\"/team/index.aspx?typeid=2\">失败团购</a><span></span></li>");
                }
                this.strTitle = "成功往期团购";
            }
            if (base.Request["typeid"].ToString() == "2")
            {
                sb.Append("<li ><a href=\"/team/index.aspx?typeid=1\">今日团购</a><span></span></li>");
                sb.Append("<li><a href=\"/team/index.aspx?typeid=3\">成功团购</a><span></span></li>");
                if ((base.ASSystem != null) && (base.ASSystem.Displayfailure == 1))
                {
                    sb.Append("<li  class=\"current\"><a href=\"/team/index.aspx?typeid=2\">失败团购</a><span></span></li>");
                }
                this.strTitle = "失败往期团购";
            }
        }
    }

    private void initPage()
    {
        string where = "  Team_type='normal' and team.begin_time <='" + this.beginTime + "' ";
        string str = "";
        if ((base.Request["gid"] != null) && (base.Request["gid"].ToString() != ""))
        {
            str = base.Request["gid"].ToString();
        }
        if (!Utility.IsNum(str))
        {
            this.SetError("参数有误！");
        }
        else
        {
            int num;
            if ((base.Request["typeid"] != null) && (base.Request["typeid"].ToString() != ""))
            {
                string str6 = base.Request["typeid"].ToString();
                if (str6 != null)
                {
                    if (!(str6 == "1"))
                    {
                        if (str6 == "3")
                        {
                            where = string.Concat(new object[] { where, " and team.end_time <='", this.endTime, "' " });
                        }
                        else if (str6 == "2")
                        {
                            where = string.Concat(new object[] { where, " and team.end_time <='", this.endTime, "' " });
                        }
                    }
                    else
                    {
                        where = string.Concat(new object[] { where, " and '", this.endTime, "' <=team.end_time  " });
                    }
                }
            }
            if (!this.result)
            {
                where = string.Concat(new object[] { where, "  and not(team.min_number>team.now_number and team.end_time <='", this.endTime, "' ) " });
            }
            if (str != "")
            {
                where = where + " and Group_id=" + str;
            }
            if ((base.Request["id"] != null) && (base.Request["id"].ToString() != ""))
            {
                where = where + " and id=" + base.Request["id"].ToString();
            }
            Team team = new Team();
            StringBuilder builder = new StringBuilder();
            DataSet set = DbHelperSQL.Page(10, this.pager.CurrentPageIndex, "Team", where, " begin_time desc", "*", out num);
            if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    string currency = "";
                    int num3 = 0;
                    if (base.ASSystem != null)
                    {
                        currency = base.ASSystem.Currency;
                        num3 = int.Parse(float.Parse(base.ASSystem.Moneysave.ToString()).ToString());
                    }
                    builder.Append("<li class=\" first\">");
                    builder.Append("<p class=\"time\">" + DateTime.Parse(row["Begin_time"].ToString()).ToString("yyyy-MM-dd").ToString() + "</p>");
                    builder.Append("<h4><a href=\"/team.aspx?id=" + row["id"].ToString() + "\" title=\"" + row["Title"].ToString() + "\" target=\"_blank\">" + row["Title"].ToString() + "</a></h4>");
                    builder.Append("<div class=\"pic\">");
                    if (Convert.ToDateTime(row["End_time"].ToString()) >= this.endTime)
                    {
                        if (Convert.ToInt32(row["Max_number"].ToString()) != 0)
                        {
                            if (Convert.ToInt32(row["Now_number"].ToString()) >= Convert.ToInt32(row["Max_number"].ToString()))
                            {
                                builder.Append("<div class='isopen'></div>");
                            }
                            else
                            {
                                builder.Append("<div class='normal_on'></div>");
                            }
                        }
                        else
                        {
                            builder.Append("<div class='normal_on'></div>");
                        }
                    }
                    else
                    {
                        builder.Append("<div class='normal_off' ></div>");
                    }
                    builder.Append("<a href=\"/team.aspx?id=" + row["id"].ToString() + "\" class=\"isopenlink\" title=\"" + row["Title"].ToString() + "\" target=\"_blank\"> width=\"200\" height=\"121\"></a>");
                    builder.Append("<a href=\"/team.aspx?id=" + row["id"].ToString() + "\" title=\"" + row["Title"].ToString() + "\" target=\"_blank\"><img alt=\"" + row["Title"].ToString() + "\" src=\"" + row["Image"].ToString() + "\" width=\"200\" height=\"121\"></a>");
                    builder.Append("</div>");
                    builder.Append("<div class=\"info\">");
                    builder.Append("<p class=\"total\"><strong class=\"count\">" + row["Now_number"] + "</strong>人购买</p>");
                    builder.Append("<p class=\"total\"></p>");
                    string discount = WebSiteHelper.GetDiscount(Helper.GetDecimal(row["Market_price"], 0M), Helper.GetDecimal(row["Team_price"], 0M));
                    builder.Append("<p class=\"price\">原价：<strong class=\"old\"><span class=\"money\">\x00a5</span>" + base.GetMoney(row["Market_price"]) + "</strong><br />折扣：<strong class=\"discount\">" + discount + "</strong><br />现价：<strong><span class=\"money\">\x00a5</span>" + base.GetMoney(row["Team_price"]) + "</strong><br />节省：<strong><span class=\"money\">\x00a5</span>" + base.GetMoney(float.Parse(row["Market_price"].ToString()) - float.Parse(row["Team_price"].ToString())) + "</strong><br /></p>");
                    builder.Append("</div>");
                    if (num3 == 1)
                    {
                        float price = (float.Parse(row["Market_price"].ToString()) - float.Parse(row["Team_price"].ToString())) * float.Parse(row["Now_number"].ToString());
                        builder.Append("<div id=\"info_a\">共为用户节省:<strong class=\"count\">" + base.GetMoney(price) + "</strong></div>");
                    }
                    builder.Append("</li>");
                }
            }
            this.ltTeam.Text = builder.ToString();
            this.pager.RecordCount = num;
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        base.SetTitle("往期团购");
        this.initNav();
        if (!this.Page.IsPostBack)
        {
            this.initPage();
        }
    }

    protected void pager_PageChanged(object sender, EventArgs e)
    {
        this.initPage();
    }

}